package com.sfx.shortLink.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sfx.shortLink.dao.entity.LinkOsStatsDO;
import com.sfx.shortLink.dto.req.ShortLinkGroupStatsReqDTO;
import com.sfx.shortLink.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface ILinkOsStatsMapper extends BaseMapper<LinkOsStatsDO> {


    // unique (full_short_url, gid, date, os)
    @Insert("INSERT INTO t_link_os_stats(full_short_url, gid, date, cnt, os, create_time, update_time, del_flag)" +
            "VALUES( #{linkOsStats.fullShortUrl}, #{linkOsStats.gid}, #{linkOsStats.date}, #{linkOsStats.cnt},#{linkOsStats.os}," +
            "NOW(), NOW(), 0) ON DUPLICATE KEY UPDATE " +
            "cnt = cnt + #{linkOsStats.cnt};")
    void osStats(@Param("linkOsStats") LinkOsStatsDO linkOsStats);


    @Select("SELECT os ,SUM(cnt) as cnt " +
            "FROM t_link_os_stats " +
            "WHERE full_short_url = #{param.fullShortUrl} AND gid = #{param.gid} AND date between #{param.startDate} AND #{param.endDate} " +
            "group by full_short_url,gid;"
    )
    List<LinkOsStatsDO> listOsStatsByShortLink(@Param("param") ShortLinkStatsReqDTO param);

    @Select("SELECT os ,SUM(cnt) as cnt " +
            "FROM t_link_os_stats " +
            "WHERE gid = #{param.gid} AND date between #{param.startDate} AND #{param.endDate} " +
            "group by gid;"
    )
    List<LinkOsStatsDO> listOsStatsByShortLinkGroup(@Param("param") ShortLinkGroupStatsReqDTO param);
}
